package org.tmsps.ne4spring.base.dto;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.Data;
import org.tmsps.ne4spring.base.ne.NePage;
import org.tmsps.ne4spring.base.ne.NeSort;
import org.tmsps.ne4spring.utils.ChkUtil;

/**
 * .Extjs 的BaseQuery插件
 *
 * @author
 */
@Data
public abstract class BaseExtjsQuery implements BaseQuery {

	private int limit = 2;
	private int page = 1;
	private String sort;

	public NeSort getSort() {
		if (ChkUtil.isNotNull(sort) && !"{}".equals(sort.trim())) {
			JSONArray sorterArray = JSONArray.parseArray(sort);
			if (sorterArray.size() > 0) {
				JSONObject sorter = sorterArray.getJSONObject(0);
				String porterty = sorter.getString("property");
				String direction = sorter.getString("direction");

				NeSort neSort = new NeSort(porterty, direction);
				return neSort;
			}
		}
		return null;
	}

	public NePage getPage() {
		NePage nePage = new NePage();
		nePage.setPageNumber(page);
		nePage.setPageSize(limit);
		return nePage;
	}

}
